<?php
function makeSign($params,$signKey){
    $preString = makePreString($params,$signKey);
    $sign = md5($preString);
    // echo $sign;exit;
    return $sign;
}

function makePreString($params,$signKey){
    $params['sign_key'] = $signKey;
    $params = filterParams($params);
    ksort($params);
    // print_r($params);
    $preString = urldecode(http_build_query($params));
    // echo $preString;
    return $preString;
}

function filterParams($params=[]){
    $filtered = array_filter(
        $params,
        function($value,$key){
            if(in_array($value,['',null,false],true)){
                return false;
            }
            if(in_array($key,['sign'])){
                return false;
            }
            return true;
        },
        ARRAY_FILTER_USE_BOTH
    );
    return $filtered;
}

/**
 * 请求接口返回内容
 * @param  string $url [请求的URL地址]
 * @param  string $params [请求的参数]
 * @param  int $ipost [是否采用POST形式]
 * @return  string
 */
function juhecurl($url,$params=false,$ispost=0,$header=''){
    $httpInfo = array();
    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
    curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
    curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
    curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4); 
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
    if( $ispost )
    {
        curl_setopt( $ch , CURLOPT_POST , true );
        curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
        curl_setopt( $ch , CURLOPT_URL , $url );
    }
    else
    {
        if($params){
            curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
        }else{
            curl_setopt( $ch , CURLOPT_URL , $url);
        }
    }
    //设置header头
    if(!empty($header)){
        curl_setopt($ch,CURLOPT_HTTPHEADER,$header);
    }
    else{
        curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-Type:application/json;charset=UTF-8'));
    }
    $response = curl_exec( $ch );
    if ($response === FALSE) {
        //echo "cURL Error: " . curl_error($ch);
        return curl_error($ch);
    }
    $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
    $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
    curl_close( $ch );
    return $response;
}


function httpPost($url,$data){ // 模拟提交数据函数      
    $curl = curl_init(); // 启动一个CURL会话      
    curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址                  
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查       
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器      
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转      
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer      
    curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求      
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包       
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环      
    curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容      
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
    curl_setopt($curl,CURLOPT_HTTPHEADER,array('Content-Type:application/json;charset=UTF-8'));      
    $tmpInfo = curl_exec($curl); // 执行操作      
    $info = curl_getinfo($curl);
//     print_r($info);exit;
    if (curl_errno($curl)) {      
       echo 'Errno'.curl_error($curl);      
    }      
    curl_close($curl); // 关键CURL会话      
    return $tmpInfo; // 返回数据      
} 

/**
 * 接口提交数据返回成功
 * @param $data 
 * @param string $msg
 * @param int $code
 */
function okMsg($data = [], $msg = 'success', $code = 200)
{
    header('Content-Type:application/json; charset=utf-8');
    $out = [
        'code' => $code,
        'msg' => $msg,
        'data' => $data,
    ];
    exit(json_encode($out, JSON_UNESCAPED_UNICODE));
}


/**
 * 接口提交数据返回错误
 * @param string $msg
 * @param int $code (0:失败，302:失败,有返回数据)
 * @param $data
 */
function errMsg($msg = 'error', $code = 0, $data = [])
{
    header('Content-Type:application/json; charset=utf-8');
    $out = [
        'code' => $code,
        'msg' => $msg,
    ];
    if ($data) {
        $out['data'] = $data;
    }
    exit(json_encode($out, JSON_UNESCAPED_UNICODE));
}
function loginMsg($msg = ""){
    header('Content-Type:application/json; charset=utf-8');
    $out = [
        'code' => 0,
        'msg' => $msg,
        'url'=>url("index/index",'',true,true)
    ];

    exit(json_encode($out, JSON_UNESCAPED_UNICODE));
}